package com.becca.cake.controller.system;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.becca.cake.core.common.BaseAction;
import com.becca.cake.model.system.PostDO;
import com.becca.cake.service.system.IUserPostService;
import com.becca.cake.util.JsonUtil;
import com.becca.cake.util.StringUtil;

/**
 * 岗位管理
 * @author hejinguo
 * @version $Id: UserPostAction.java, v 0.1 2013-12-17 下午12:21:40
 */
@Controller
@RequestMapping("/systemPost")
public class UserPostAction extends BaseAction {
    private final static Log logger = LogFactory.getLog(UserPostAction.class);
    @Autowired
    private IUserPostService iUserPostService;

    /**
     * 岗位管理中查询岗位树
     * @author hejinguo
     * @return
     */
    @RequestMapping(value = "/selectPostTree", method = RequestMethod.POST)
    public @ResponseBody
    String selectPostTree() {
        try {
            return this.iUserPostService.selectPostTree();
        } catch (Exception e) {
            logger.error("岗位管理中查询岗位树失败!", e);
            return JsonUtil.returnJsonUnSuccess("岗位管理中查询岗位树失败!");
        }
    }

    /**
     * 根据ID查询岗位信息
     * @author hejinguo
     * @param postId
     * @return
     */
    @RequestMapping(value = "/selectPostMsgById", method = RequestMethod.POST)
    public @ResponseBody
    String selectPostMsgById(@RequestParam(value = "postId", required = false) String postId) {
        if (StringUtil.isNotEmpty(postId)) {
            try {
                return this.iUserPostService.selectPostMsgById(postId);
            } catch (Exception e) {
                logger.error("", e);
                return null;
            }
        } else {
            logger.error("查询岗位信息时ID不能为空!");
            return JsonUtil.returnJsonUnSuccess("查询岗位信息时ID不能为空!");
        }
    }

    /**
     * 添加岗位信息
     * @author hejinguo
     * @param postDO
     * @return
     */
    @RequestMapping(value = "/insertPost", method = RequestMethod.POST)
    public @ResponseBody
    String insertPost(@ModelAttribute PostDO postDO) {
        if (postDO != null) {
            postDO.setCreatedMan(this.getUserSeesion().getTrueName());
            try {
                this.iUserPostService.insertPost(postDO);
                return JsonUtil.returnJsonSuccess("添加成功!");
            } catch (Exception e) {
                logger.error("添加失败!", e);
                return JsonUtil.returnJsonUnSuccess("添加失败!");
            }
        } else {
            logger.error("添加岗位信息时信息为空!");
            return JsonUtil.returnJsonUnSuccess("添加岗位信息时信息为空!");
        }
    }

    /**
     * 修改岗位信息
     * @author hejinguo
     * @param postDO
     * @return
     */
    @RequestMapping(value = "/updatePostById", method = RequestMethod.POST)
    public @ResponseBody
    String updatePostById(@ModelAttribute PostDO postDO) {
        if (postDO != null) {
            postDO.setUpdatedMan(this.getUserSeesion().getTrueName());
            try {
                this.iUserPostService.updatePostById(postDO);
                return JsonUtil.returnJsonSuccess("修改成功!");
            } catch (Exception e) {
                logger.error("修改失败!", e);
                return JsonUtil.returnJsonUnSuccess("修改失败!");
            }
        } else {
            logger.error("修改岗位信息时信息为空!");
            return JsonUtil.returnJsonUnSuccess("修改岗位信息时信息为空!");
        }
    }

}
